<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//

require_once(TANGRA_MAIN_DIR.'form/form_ctrl.class.php');

require_once(TANGRA_MAIN_DIR.'exceptions/tangra_module_exception.class.php');


require_once($WSC->get_site_inc_dir().'modules/ajax_ctrl/ajax_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/chocolate_ajax_form_ctrl/chocolate_ajax_form_ctrl.class.php');


abstract class Chocolate_Ajax_Form_Ctrl_With_Object extends Chocolate_Ajax_Form_Ctrl {
	public function run() {
		$context = $this->get__context();

		$form_ctrl = $this->get_form_ctrl();


		$event_submit = $form_ctrl->get_form_submit_event();
		$event_add_new = $form_ctrl->get_add_new_event();
		$event_object_id = $form_ctrl->get_object_id_event();


		if ($event_submit->is_it_me($context)) {
			$this->process_submit();
		} else {
			$object_id = $event_object_id->is_it_me($context);
			if ($object_id) {
				$this->process_receiving_object_id($object_id);
			} else {
				if ($event_add_new->is_it_me($context)) {
					$this->process_add_new();
				} else {
					throw new Tangra_Module_Exception('Unexpected mode.');
				}
			}
		}
	}


	protected function process_submit() {
		$context = $this->get__context();

		$form_ctrl = $this->get_form_ctrl();
		$rez = $form_ctrl->process_submit($context);
		$ret['result_code'] = Ajax_Ctrl::STATUS_OK;
		$ret['form_code'] = $rez;
		$html = '';
		switch ($rez) {
			case Form_Ctrl::SUBMIT_RESULT_OK:
				$additional_json_params = $this->on_good_submit();
				$this->add_additional_response_parameters($additional_json_params);
				break;
			case Form_Ctrl::SUBMIT_RESULT_ERROR:
				$additional_json_params = $this->on_bad_submit();
				$this->add_additional_response_parameters($additional_json_params);

				$form_ctrl->prepare_for_show();
				$html = $this->get_html();
				break;
			default:
				throw new Tangra_Module_Exception('Unexpected result code: '.$rez);
				break;
		}

		$this->send_response($ret, $html);
	}


	protected function process_receiving_object_id($object_id) {
		tangra_if_not_int_throw_e($object_id);

		$this->on_received_id($object_id);

		$dbc = $this->get_dbc();

		$form_ctrl = $this->get_form_ctrl();
		$object = $form_ctrl->get_object();


		if ($object->load_by_id($dbc, $object_id)) {
			$form_ctrl = $this->get_form_ctrl();
			$form_ctrl->process_object_id($object_id);

			$rez = array('result_code' => Ajax_Ctrl::STATUS_OK);

			$html = $this->get_html();
			$this->send_response($rez, $html);
		} else {
			//TODO think about replacing exception with return error code
			throw new Tangra_Module_Exception('Can not find object with id = '.$object_id);
		}

		$dbc->disconnect();
	}


	protected function process_add_new() {
		$form_ctrl = $this->get_form_ctrl();
		$form_ctrl->process_add_new();
		$html = $this->get_html();
		$rez = array('result_code' => Ajax_Ctrl::STATUS_OK);
		$this->send_response($rez, $html);
	}


	protected function on_bad_submit() {

	}


	protected function on_received_id($id) {

	}
}
